package com.cskt.petproject178.controller;

import com.cskt.petproject178.config.AliPayAutoConfigProperties;
import com.cskt.petproject178.pojo.vo.ResultVO;
import com.cskt.petproject178.service.third.AliPayService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 支付宝支付控制器
 *
 * @author 27013
 **/

@RestController
@RequestMapping(value = "/oms/pay/aliPay")
public class AliPayController {

    @Resource
    private AliPayService aliPayService;

    @GetMapping(value = "/{orderNo}")
    public ResultVO<?> aliPay(@PathVariable(value = "orderNo") String orderNo){
        String alipayPage  = aliPayService.aliPayTrade(orderNo);
    return ResultVO.OK(alipayPage);
    }


   @PostMapping(value = "/notify")
   public void notify(HttpServletRequest request){
       //获取支付宝post过来的反馈信息
       Map<String, String> params = new HashMap<>();
       Map requestParams =request.getParameterMap();
       for(Iterator iter = requestParams.keySet().iterator(); iter.hasNext();){
           String name=iter.next().toString();
           String[] values= (String[]) requestParams.get(name);
           String valueStr="";
           for(int i =0;i<values.length;i++){
               valueStr=(i==values.length-1)?valueStr+values[i]:valueStr+values[i]+",";
           }
           params.put(name,valueStr);
       }
       aliPayService.verifyV1(params);
   }
}
